package edu.zju.gis.dldsj.service.impl;

import edu.zju.gis.dldsj.dao.StorageVisitMapper;
import edu.zju.gis.dldsj.entity.DataVisit;
import edu.zju.gis.dldsj.entity.vo.StorageWithVisit;
import edu.zju.gis.dldsj.mapper.StorageMapper;
import edu.zju.gis.dldsj.model.Storage;
import edu.zju.gis.dldsj.service.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yanlo yanlong_lee@qq.com
 * @version 1.0 2018/08/09
 */
@Service
public class StorageServiceImpl implements StorageService {
    @Autowired
    private StorageMapper storageMapper;
    @Autowired
    private StorageVisitMapper visitMapper;

    @Override
    public Storage select(String pk) {
        return storageMapper.selectByPrimaryKey(pk);
    }

    @Override
    public int insert(Storage storage) {
        return storageMapper.insertSelective(storage);
    }

    @Override
    public void update(Storage storage) {
        storageMapper.updateByPrimaryKeySelective(storage);
    }

    @Override
    public void delete(String integer) {
        storageMapper.deleteByPrimaryKey(integer);
    }

    @Override
    public boolean isExist(String integer) {
        return storageMapper.selectByPrimaryKey(integer) != null;
    }

    @Override
    public List<Storage> getByPage(int offset, int size) {
        return storageMapper.selectByPage(offset, size);
    }

    @Override
    public int getTotalSize() {
        return storageMapper.selectCount();
    }

    @Override
    public DataVisit getVisit(String dataId) {
        return visitMapper.selectByDataId(dataId);
    }

    @Override
    public List<DataVisit> getVisit(List<String> dataIdList) {
        return visitMapper.selectByDataIdList(dataIdList);
    }

    @Override
    public List<StorageWithVisit> getByVisitAsc(int offset, int size) {
        List<DataVisit> dataVisits = visitMapper.selectByVisitAsc(offset, size);
        return dataVisits.stream().map(visit -> new StorageWithVisit(storageMapper.selectByPrimaryKey(visit.getDataId()), visit))
                .collect(Collectors.toList());
    }

    @Override
    public List<StorageWithVisit> getByVisitDesc(int offset, int size) {
        List<DataVisit> dataVisits = visitMapper.selectByVisitDesc(offset, size);
        return dataVisits.stream().map(visit -> new StorageWithVisit(storageMapper.selectByPrimaryKey(visit.getDataId()), visit))
                .collect(Collectors.toList());
    }

    @Override
    public int getSizeByComment(String comment) {
        return storageMapper.getByComment(comment).size();
    }

    @Override
    public List<StorageWithVisit> getByCommentAndVisitDesc(String comment, int offset, int size) {
        List<Storage> storages = storageMapper.getByComment(comment);
        List<StorageWithVisit> result = storages.stream().map(storage -> new StorageWithVisit(storage, visitMapper.selectByDataId(storage.getPk_meta_id())))
                .sorted(Comparator.comparing(StorageWithVisit::getVisit).reversed())
                .collect(Collectors.toList());
        if (offset > result.size())
            return Collections.emptyList();
        else {
            if ((offset + size) > result.size())
                size = result.size() - offset;
            return result.subList(offset, offset + size);
        }
    }
}
